<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="110px"
        v-loading="formLoading"
    >
                  <el-form-item label="账户ID" prop="accountId">
                    <el-input v-model="formData.accountId" placeholder="请输入账户ID"/>
                  </el-form-item>
                  <el-form-item label="广告主id" prop="advertiserId">
                    <el-input v-model="formData.advertiserId" placeholder="请输入广告主id"/>
                  </el-form-item>
                  <el-form-item label="广告主名称" prop="advertiserName">
                    <el-input v-model="formData.advertiserName" placeholder="请输入广告主名称"/>
                  </el-form-item>
                  <el-form-item label="广告计划ID" prop="promotionId">
                    <el-input v-model="formData.promotionId" placeholder="请输入广告计划ID"/>
                  </el-form-item>
                  <el-form-item label="广告计划名称" prop="promotionName">
                    <el-input v-model="formData.promotionName" placeholder="请输入广告计划名称"/>
                  </el-form-item>
                  <el-form-item label="线索类型" prop="clueType">
                    <el-select v-model="formData.clueType" placeholder="请选择线索类型">
                          <el-option label="请选择字典生成" value=""/>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="分配类型" prop="allocationStatus">
                    <el-radio-group v-model="formData.allocationStatus">
                          <el-radio label="1">请选择字典生成</el-radio>
                    </el-radio-group>
                  </el-form-item>
                  <el-form-item label="互动场景" prop="actionType">
                    <el-select v-model="formData.actionType" placeholder="请选择互动场景">
                          <el-option label="请选择字典生成" value=""/>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="线索名称" prop="clueName">
                    <el-input v-model="formData.clueName" placeholder="请输入线索名称"/>
                  </el-form-item>
                  <el-form-item label="年龄" prop="age">
                    <el-input v-model="formData.age" placeholder="请输入年龄"/>
                  </el-form-item>
                  <el-form-item label="电话" prop="telephone">
                    <el-input v-model="formData.telephone" placeholder="请输入电话"/>
                  </el-form-item>
                  <el-form-item label="线索负责人名称" prop="clueOwnerName">
                    <el-input v-model="formData.clueOwnerName" placeholder="请输入线索负责人名称"/>
                  </el-form-item>
                  <el-form-item label="省份" prop="provinceName">
                    <el-input v-model="formData.provinceName" placeholder="请输入省份"/>
                  </el-form-item>
                  <el-form-item label="市" prop="cityName">
                    <el-input v-model="formData.cityName" placeholder="请输入市"/>
                  </el-form-item>
                  <el-form-item label="区县" prop="countyName">
                    <el-input v-model="formData.countyName" placeholder="请输入区县"/>
                  </el-form-item>
                  <el-form-item label="详细地址" prop="address">
                    <el-input v-model="formData.address" placeholder="请输入详细地址"/>
                  </el-form-item>
                  <el-form-item label="原始线索id" prop="adClueId">
                    <el-input v-model="formData.adClueId" placeholder="请输入原始线索id"/>
                  </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
  import * as ClueLogApi from '@/api/ad/cluelog'

  const {t} = useI18n() // 国际化
  const message = useMessage() // 消息弹窗

  const dialogVisible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
  const formType = ref('') // 表单的类型：create - 新增；update - 修改
  const formData = ref({
                      id: undefined,
                      accountId: undefined,
                      advertiserId: undefined,
                      advertiserName: undefined,
                      promotionId: undefined,
                      promotionName: undefined,
                      clueType: undefined,
                      allocationStatus: undefined,
                      actionType: undefined,
                      clueName: undefined,
                      age: undefined,
                      telephone: undefined,
                      clueOwnerName: undefined,
                      provinceName: undefined,
                      cityName: undefined,
                      countyName: undefined,
                      address: undefined,
                      adClueId: undefined
  })
  const formRules = reactive({
                  accountId: [{
              required: true, message: '账户ID不能为空', trigger: 'blur' }],
                  clueType: [{
              required: true, message: '线索类型不能为空', trigger: 'change'
 }],
                  allocationStatus: [{
              required: true, message: '分配类型不能为空', trigger: 'blur' }],
                  actionType: [{
              required: true, message: '互动场景不能为空', trigger: 'change'
 }],
                  clueName: [{
              required: true, message: '线索名称不能为空', trigger: 'blur' }],
                  age: [{
              required: true, message: '年龄不能为空', trigger: 'blur' }]
  })
  const formRef = ref() // 表单 Ref

  /** 打开弹窗 */
  const open = async (type: string, id?: number) => {
    dialogVisible.value = true
    dialogTitle.value = t('action.' + type)
    formType.value = type
    resetForm()
    // 修改时，设置数据
    if (id) {
      formLoading.value = true
      try {
        formData.value = await ClueLogApi.getClueLog(id)
      } finally {
        formLoading.value = false
      }
    }
  }
  defineExpose({open}) // 提供 open 方法，用于打开弹窗

  /** 提交表单 */
  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
  const submitForm = async () => {
    // 校验表单
    if (!formRef) return
    const valid = await formRef.value.validate()
    if (!valid) return
    // 提交请求
    formLoading.value = true
    try {
      const data = formData.value as unknown as ClueLogApi.ClueLogVO
      if (formType.value === 'create') {
        await ClueLogApi.createClueLog(data)
        message.success(t('common.createSuccess'))
      } else {
        await ClueLogApi.updateClueLog(data)
        message.success(t('common.updateSuccess'))
      }
      dialogVisible.value = false
      // 发送操作成功的事件
      emit('success')
    } finally {
      formLoading.value = false
    }
  }

  /** 重置表单 */
  const resetForm = () => {
    formData.value = {
                        id: undefined,
                        accountId: undefined,
                        advertiserId: undefined,
                        advertiserName: undefined,
                        promotionId: undefined,
                        promotionName: undefined,
                        clueType: undefined,
                        allocationStatus: undefined,
                        actionType: undefined,
                        clueName: undefined,
                        age: undefined,
                        telephone: undefined,
                        clueOwnerName: undefined,
                        provinceName: undefined,
                        cityName: undefined,
                        countyName: undefined,
                        address: undefined,
                        adClueId: undefined
    }
    formRef.value?.resetFields()
  }
</script>
